package HomeWork.Question2;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;

/*
 模拟成绩管理系统。有五个学生，每个学生有3门课的成绩，从键盘输入以上数据（包括学生号，姓名，三门课成绩），
 计算出平均成绩，把原有的数据和计算出的平均分数存放在D盘文件 "stuData.txt"中。
 */

class Grade_Management {
    public static void main(String[] args) {
        String id;
        String name;
        double first_source;
        double second_source;
        double third_source;
        double average_score;
        String data;
        int i = 1;
        try(Scanner scanner = new Scanner(System.in)){
            while (true) {
                System.out.print("请输入第"+i+"个学生的学号：");
                id = scanner.next();
                System.out.print("请输入第"+i+"个学生的姓名：");
                name = scanner.next();
                System.out.print("请输入该学生的第1个成绩：");
                first_source = scanner.nextDouble();
                System.out.print("请输入该学生的第2个成绩：");
                second_source = scanner.nextDouble();
                System.out.print("请输入该学生的第3个成绩：");
                third_source = scanner.nextDouble();
                System.out.println();
        
                average_score = (first_source + second_source + third_source) / 3;
                data = id + '\n' + name + '\n' + first_source + '\n' + second_source + '\n' + third_source + '\n' +average_score;
                Grade_Management.write_grade_to_file("./HomeWork/Question2/stuData.txt",data);
                i++;
            }
        } catch (Exception e) {
            System.out.println("输入错误！");
        }
    }
    
    static void write_grade_to_file(String file_name,String data) {
        try (                                                       //将成绩写入文件
            FileWriter fileWriter = new FileWriter(file_name,true);
            BufferedWriter bufferedWriter = new BufferedWriter(fileWriter)){
                bufferedWriter.write(data);         // 写入数据
                bufferedWriter.newLine();           // 换行
                bufferedWriter.close();             // 关闭缓冲流
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
